﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IO = System.IO;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.Xml;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.WorksheetElements;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.StyleElements;
using System.Xml;
using DocumentFormat.OpenXml.Lite.Core.Interfaces;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Base
{
    [XmlRoot("styleSheet", Namespace = Constants.ExcelMain)]
    public class StyleSheet : XmlSerializable, IOfficeElement, ISerializable
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        [Optional]
        [XmlElement("numFmts", Namespace = Constants.ExcelMain)]
        public NumberFormats NumberFormats { get; set; }

        [Optional]
        [XmlElement("fonts", Namespace = Constants.ExcelMain)]
        public Fonts Fonts { get; set; }

        [Optional]
        [XmlElement("fills", Namespace = Constants.ExcelMain)]
        public Fills Fills { get; set; }

        [Optional]
        [XmlElement("borders", Namespace = Constants.ExcelMain)]
        public Borders Borders { get; set; }

        [Optional]
        [XmlElement("cellStyleXfs", Namespace = Constants.ExcelMain)]
        public RecordFormats RecordFormats { get; set; }

        [Optional]
        [XmlElement("cellXfs", Namespace = Constants.ExcelMain)]
        public CellFormats CellFormats { get; set; }

        [Optional]
        [XmlElement("cellStyles", Namespace = Constants.ExcelMain)]
        public CellStyles CellStyles { get; set; }

        [Optional]
        [XmlElement("dxfs", Namespace = Constants.ExcelMain)]
        public Formats Formats { get; set; }

        [Optional]
        [XmlElement("tableStyles", Namespace = Constants.ExcelMain)]
        public TableStyles TableStyles { get; set; }

        [Optional]
        [XmlElement("colors", Namespace = Constants.ExcelMain)]
        public Colors Colors { get; set; }

        [Optional]
        [XmlElement("extLst", Namespace = Constants.ExcelMain)]
        public ExtensionList ExtensionList { get; set; }

        public StyleSheet() { }

        #region ISerializable members

        public byte[] Serialize()
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", Constants.ExcelMain);
            //ns.Add("xml", Constants.Xml);
            XmlSerializer serializer = new XmlSerializer(this.GetType());
            using (var stream = new IO.MemoryStream())
            {
                using (var writer = new IO.StreamWriter(stream, Encoding.UTF8))
                {
                    serializer.Serialize(writer, this, ns);
                    return stream.ToArray();
                }
            }
        }

        public void Deserialize(System.IO.Stream content)
        {
            XmlSerializer serializer = new XmlSerializer(this.GetType(), Constants.ExcelMain);
            var ss = serializer.Deserialize(content) as StyleSheet;

            this.Borders = ss.Borders;
            this.CellFormats = ss.CellFormats;
            this.CellStyles = ss.CellStyles;
            this.Colors = ss.Colors;
            this.ExtensionList = ss.ExtensionList;
            this.Fills = ss.Fills;
            this.Fonts = ss.Fonts;
            this.Formats = ss.Formats;
            this.NumberFormats = ss.NumberFormats;
            this.RecordFormats = ss.RecordFormats;
            this.TableStyles = ss.TableStyles;
        }

        #endregion
    }
}
